"""
jackson.cdn.views.client

This file contains wrappers for the client API.
"""
### Start of imports

# This line just better work...
from jackson import debug

# There's debug stuff for the rest
debug.pre_import(__name__)

debug.importing(__name__, 'jackson.extjson.ext_json as json')
import jackson.extjson.ext_json as json

debug.importing(__name__, 'urllib2', 3)
import urllib2

debug.importing(__name__, 'client_api from jackson.cdn')
from jackson.cdn import client_api

debug.importing(__name__, 'HttpResponse from django.http')
from django.http import HttpResponse

debug.post_import(__name__)

### End of imports

def get_url_handler(request):
    context = [__name__, 'get_url_handler()']
    debug.enter(context, None, 2)
    try:
        debug.comment(context, 'Unpacking request', 2)
        the_key = json.loads(request.REQUEST['data'])
        debug.comment(context, 'Calling into client_api', 2)
        result = client_api.get_url(the_key)
        response = HttpResponse(json.dumps(result), mimetype='application/json')
    except Exception, e:
        debug.exception(context, str(e))
    
    debug.leave(context, { 'result' : result})
    return response

def register_key_handler(request):
    context = [__name__, 'register_key_handler()']
    debug.enter(context, None, 2)
    try:
        debug.comment(context, 'Unpacking request', 2)
        req = json.loads(request.REQUEST['data'])
        debug.comment(context, 'Calling into client_api', 2)
        result = client_api.register_key(req['key'], req['url'])
        response = HttpResponse(json.dumps(result), mimetype='application/json')
    except Exception, e:
        debug.exception(context, str(e))
        result = { 'exception' : str(e) }
        response = HttpResponse(json.dumps(result), mimetype='application/json')
    debug.leave(context, result)
    return response